//
//  RegisteredPopulationListViewController.m
//  Utils
//
//  Created by enghou on 2018/4/20.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "RegisteredPopulationListViewController.h"
#import "PopulationContainerViewController.h"
#import "PopulationSearchViewController.h"
#import "LocalPersistent.h"
@interface RegisteredPopulationListViewController ()

@property(nonatomic,strong)UIButton *fBtn;//悬浮的按钮

@end

@implementation RegisteredPopulationListViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        self.cellClass = @"PopulationTableViewCell";
        self.modelClsName = @"PopulationItemModel";
        self.path = kPopulationRegister;
    }
    return self;
}

-(NSDictionary *)param{
    NSMutableDictionary *param = [@{@"page":[NSString stringWithFormat:@"%li",(long)self.page],@"rows":@"10"} mutableCopy];
    if ([self.orgId length] > 0) {
        [param setObject:self.orgId forKey:@"searchVo.orgId"];
    }
    if (self.choosenParam) {
        [param addEntriesFromDictionary:self.choosenParam];
    }
    return param;
}

-(NSString *)orgId{
    return [[LocalPersistent sharedInstance]contentForKey:@"orgId"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *search = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchAction)];
    search.tintColor = [UIColor whiteColor];
    UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_nav_position"] style:UIBarButtonItemStylePlain target:self action:@selector(chooseOrg)];
    edit.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItems = @[search,edit];
    
    self.fBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.fBtn.frame = CGRectMake(self.view.width - 60, self.view.height - 40 - 40, 40, 40);
    [self.fBtn setImage:[UIImage imageNamed:@"icon_add"] forState:UIControlStateNormal];
    UIWindow *window = [[UIApplication sharedApplication]delegate].window;
    [window addSubview:self.fBtn];
    [self.fBtn addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    self.fBtn.hidden = YES;
}

-(void)addAction{
    [self pushWithId:nil];
}

-(void)searchAction{
    PopulationSearchViewController *p = (PopulationSearchViewController *)[VCLoader vcWithName:@"PopulationSearchViewController"];
    p.theDelete = self;
    p.title = @"户籍人口查询";
    [self push:p];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.orgId) {
        self.fBtn.hidden = NO;
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fBtn.hidden = YES;
}

-(void)pushWithId:(NSString *)ids{
    PopulationContainerViewController *pvc = [[PopulationContainerViewController alloc]init];
    pvc.title = @"户籍人口详情";
    pvc.ids = ids;
    pvc.theDelete = self;
    [self push:pvc];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PopulationItemModel *model = [self.source objectAtIndex:indexPath.row];
    [self pushWithId:model.ids];
}

-(void)fetchModel:(id)model{
    if ([model isKindOfClass:[NSString class]]) {
        self.orgId = model;
        //
        self.fBtn.hidden = NO;
    }else if ([model isKindOfClass:[NSDictionary class]]){
        self.choosenParam = model;
    }
    [self beginRefresh];
}

-(void)dealloc{
    [self.fBtn removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
